package com.xhwl.logistics.controller;

import com.alibaba.fastjson.JSONObject;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.logistics.bo.VehicleCarChargeBO;
import com.xhwl.logistics.bo.cent.VehicleIntegratedConfigBO;
import com.xhwl.logistics.bo.input.QueryVehicleCarChargeBO;
import com.xhwl.logistics.converter.mapstruct.VehicleCarChargeConverter;
import com.xhwl.logistics.dto.CarBillInfoDTO;
import com.xhwl.logistics.dto.query.VehicleCarChargeDTO;
import com.xhwl.logistics.enums.VehicleParkingLotHandlerEnum;
import com.xhwl.logistics.service.business.VehicleCarChargeBusinessService;
import com.xhwl.logistics.service.cent.QueryDeviceService;
import com.xhwl.logistics.strategy.IVehicleParkingLotHandler;
import com.xhwl.logistics.vo.VehicleCarChargeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

//@RestController
public class VehicleCarChargeController {

    @Autowired
    private VehicleCarChargeConverter vehicleCarChargeConverter;

    @Autowired
    private VehicleCarChargeBusinessService vehicleCarChargeBusinessService;

    @Autowired
    private QueryDeviceService queryDeviceService;

    @Autowired
    private ApplicationContext applicationContext;

    @PostMapping("/car-charge/list")
    public ResultJson<VehicleCarChargeVO> carCharge(@RequestBody VehicleCarChargeDTO vehicleCarChargeDTO) {
        QueryVehicleCarChargeBO queryVehicleCarChargeBO = vehicleCarChargeConverter.vehicleCarChargeDTO2QueryVehicleCarChargeBO(vehicleCarChargeDTO);

        VehicleCarChargeBO vehicleCarChargeBO = vehicleCarChargeBusinessService.carCharge(queryVehicleCarChargeBO);

        VehicleCarChargeVO vehicleCarChargeVO = vehicleCarChargeConverter.vehicleCarChargeBO2VehicleCarChargeVO(vehicleCarChargeBO);
        return ResultJson.successWithResult(vehicleCarChargeVO);
    }

    /**
     * 通过车牌号查询是否存在订单
     */
    @PostMapping("/car-bill/get-info")
    public ResultJson getCarBillInfo(@RequestBody CarBillInfoDTO carBillInfoDTO){
        VehicleIntegratedConfigBO vehicleIntegratedConfigBO = queryDeviceService.queryIntegratedConfigFromProjectId(carBillInfoDTO.getProjectId());
        Integer productId = vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProductId();

        IVehicleParkingLotHandler vehicleParkingLotHandler = applicationContext.getBean(VehicleParkingLotHandlerEnum.toVehicleParkingLotHandlerEnum(productId).getHandlerName(), IVehicleParkingLotHandler.class);
        String carBillInfo = vehicleParkingLotHandler.getCarBillInfo(vehicleIntegratedConfigBO, carBillInfoDTO.getPlateNumber());
        return ResultJson.successWithResult(JSONObject.parseObject(carBillInfo));
    }
}
